#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
主程序入口文件
"""

import sys
from PyQt5.QtWidgets import QApplication
from gui.main_window import MainWindow
from utils.logger import setup_logger, get_logger
from config.settings import settings

def main():
    """
    程序主入口
    """
    try:
        # 初始化日志
        setup_logger()
        logger = get_logger()
        logger.info(f'启动应用 {settings.app_name} v{settings.app_version}')
        
        # 创建QT应用
        app = QApplication(sys.argv)
        app.setApplicationName(settings.app_name)
        app.setApplicationVersion(settings.app_version)
        
        # 创建主窗口
        window = MainWindow()
        window.show()
        
        # 运行应用
        exit_code = app.exec_()
        logger.info(f'应用正常退出,退出码: {exit_code}')
        sys.exit(exit_code)
        
    except Exception as e:
        # 记录未捕获的异常
        try:
            logger = get_logger()
            logger.critical(f'程序发生致命错误: {str(e)}', exc_info=True)
        except:
            # 如果日志系统也失败,输出到stderr
            import traceback
            print(f'致命错误: {str(e)}', file=sys.stderr)
            traceback.print_exc()
        sys.exit(1)

if __name__ == "__main__":
    main()